﻿<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
	<head>
		<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
		<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
		<title>報表列印</title>
		<style>
			td{font-size:10pt;cursor:default;}
		</style>
		<script language="javascript">
		var reportControl=window.name;
		var captionStyle="font-size:20pt;font-weight:bold;";
		var titleStyle="font-size:10pt;font-weight:bold";
		var bodyOutBorderColor="#000000";
		var bodyColor="#000000";
		
		var currentPrintPage=1;
		var totalPrintPage=0;
		var PrintStartPageNo,PrintEndPageNo
		var previewWin,helpWin;
		var ReportContextArray=new Array();
		var ReportTitle="";
		var Author="";		//製表人
		var ReportDate="";	//報表日期
		var AlwaysPrintHead=false;	//是否在每頁都列印列頭。
		var HeadAlign="left";		//列頭的對齊方式
		
		var LoadedNewPage=false;		//是否載入過新的頁。
		var IsJumpPage=false			//是否執行過主表格跳轉
		var printInterval=2000;			//讀取下一頁數據所間隔的毫秒數s
		var RetryTimesCount=3;			//當訪問父視窗不成功時的最大重試次數。
		var RetryTimes=0;				//當前重試次數
		var IsPrintting=false			//目前是否處於列印中...
		var gridCurrentPageIndex=1		//父表格當前的頁碼
		var gridTotalPageCount=0;		//父表格總頁數
		var historyPage=0				//記錄跳轉頁數量，便於列印完成時恢復
		var hiddenColumns=new Array();	//不需要顯示的列，陣列中記錄的是不顯示列的索引
		
		
		function window.onhelp()
		{
			helpWin=window.open("PrintHelp.htm","Help","width=330,height=235,status=no");
			return false;
		}
		function Initialize()
		{
			//���ò���
			gridCurrentPageIndex=parseInt(window.opener.document.all(reportControl+"_CurrentPageIndex").innerText);
			gridTotalPageCount=parseInt(window.opener.document.all(reportControl+"_TotalPageIndex").innerText);
			
			//�ɴ�ӡҳ�뷶Χ
			document.all("PrintStartPageNo").value=PrintStartPageNo=gridCurrentPageIndex;
			document.all("PrintEndPageNo").value=PrintEndPageNo=gridTotalPageCount;
			
			//�������Ƶ���Ļ�м�
			screenH=screen.availHeight;
			screenW=screen.availWidth;
			x=(screenW-window.document.body.offsetWidth)/2;
			y=(screenH-window.document.body.offsetHeight)/2;
			window.moveTo(x,y);
			
			var dataControl=window.opener.document.all(reportControl);			
			var dataGrid=dataControl.childNodes[1].cloneNode(true);
			var allColumns=new Array();
			var c;
			for(c=0;c<dataGrid.rows[0].cells.length;c++)
			{
				allColumns[c]=dataGrid.rows[0].cells[c].innerText;
			}
			CreateList(allColumns);
			SetSelectedItemByValue(document.all("checkboxList1"),hiddenColumns);
		}
		function getBodyStyle()
		{
			return "width:100%;font-family:Verdana;font-size:12px;color:"+bodyColor+";border-right:2px solid "+bodyOutBorderColor+";border-top:2px solid "+bodyOutBorderColor+";border-left:2px solid "+bodyOutBorderColor+";border-bottom:2px solid "+bodyOutBorderColor+";border-collapse:collapse";
		}
		function ButtonStyleSet(Style,Element)
		{
			if(Element.getAttribute("Enabled")!=null)
			{
				if(Element.getAttribute("Enabled")=="False")
				return false
			}
			switch(Style)
			{
				case "3D":
					Element.style.borderRight="buttonshadow 1px solid";
					Element.style.borderTop="buttonhighlight 1px solid";
					Element.style.borderLeft="buttonhighlight 1px solid";
					Element.style.borderBottom="buttonshadow 1px solid";
					break;
				case "Flat":
					Element.style.borderLeft="0px";
					Element.style.borderBottom="0px";
					Element.style.borderRight="0px";
					Element.style.borderTop="0px";
					break;
				case "Cancave":
					Element.style.borderLeft="buttonshadow 1px solid";
					Element.style.borderBottom="buttonhighlight 1px solid";
					Element.style.borderRight="buttonhighlight 1px solid";
					Element.style.borderTop="buttonshadow 1px solid";
					break;
			}
		}
		function DisableBtn()
		{
			document.all("Btn_PageSet").setAttribute("Enabled","false");
			document.all("Btn_Preview").setAttribute("Enabled","false");
			document.all("Btn_Print").setAttribute("Enabled","false");
			document.all("Btn_ReportSet").setAttribute("Enabled","false");
		}
		function CreatePrintArea()
		{
			document.body.insertAdjacentHTML("beforeEnd", "<iframe name='printFrame' width='0' height='0'></iframe>");
		}
		function GetReportTitle()
		{
			if(ReportTitle=="")
			{
				var dataControl=window.opener.document.all(reportControl);
				titleGrid=dataControl.childNodes[0];
				return titleGrid.rows[0].cells[0].childNodes[0].innerText;
			}
			else
			{
				return ReportTitle;
			}
		}
		//��ȡĳҳ���������
		function GetReportContext(pageIndex)
		{
			var pageNoHTML="<table width=100%><tr><td align=right style='font-size:9pt'>第&nbsp;"+currentPrintPage.toString()+"&nbsp;頁&nbsp;共&nbsp;"+totalPrintPage.toString()+"&nbsp;頁</td></tr>";
			var tempHTML="<table bordercolor='"+bodyOutBorderColor+"' border=1 style='"+getBodyStyle()+"'>";
			if(ReportContextArray.length==0)
			{
				//������黹�ǿյģ�����뵱ǰҳ�����ݡ�
				ReportContextArray[1]=GenerateReportContext();
				return GenerateReportCaption()+GenerateReportTitle()+tempHTML+GenerateHead(true)+ReportContextArray[1]+"</table>"+pageNoHTML;
			}
			if(pageIndex==1)
			{
				return GenerateReportCaption()+GenerateReportTitle()+tempHTML+GenerateHead(true)+ReportContextArray[1]+"</table>"+pageNoHTML;
			}
			if(pageIndex>ReportContextArray.length-1)
			{
				ReportContextArray[pageIndex]=GenerateReportContext();
			}
			return tempHTML+GenerateHead(false)+ReportContextArray[pageIndex]+"</table>"+pageNoHTML;
		}
		
		//���ɱ������
		function GenerateReportCaption()
		{
			titleHTML="<p align=center><font style='"+captionStyle+"'>"+GetReportTitle()+"</font></p>";
			return titleHTML;
		}
		//��ȡ��ͷ���Ʊ�����Ϣ��
		function GenerateReportTitle()
		{
			if(Author=="" && ReportDate=="")
				return "";
			if(Author=="" &&  ReportDate!="")
				tempHTML="<table width=100%><tr><td align=right><font style='"+titleStyle+"'>製錶日期："+ReportDate+"</font></td></tr></table>";
			else
			{
				if(Author!="" && ReportDate=="")
					tempHTML="<table width=100%><tr><td align=left><font style='"+titleStyle+"'>製錶人："+Author+"</font></td></tr></table>";
				else
					tempHTML="<table width=100%><tr><td align=left><font style='"+titleStyle+"'>製錶人："+Author+"</font></td><td align=right><font style='"+titleStyle+"'>製錶日期："+ReportDate+"</font></td></tr></table>";
			}
			return tempHTML;
		}
		
		//���ɱ����ͷ
		function GenerateHead(isFirstPage)
		{
			var tempHTML="";
			var dataControl=window.opener.document.all(reportControl);			
			var dataGrid=dataControl.childNodes[1].cloneNode(true);

			if(isFirstPage || AlwaysPrintHead)
			{
				tempHTML="<tr>";
				for(c=0;c<dataGrid.rows[0].cells.length;c++)
				{
					if(!SearchElementFromArray(hiddenColumns,c))
						tempHTML+="<TD align="+HeadAlign+"><b>"+dataGrid.rows[0].cells[c].innerText+"</b></td>";				
				}
				tempHTML+="</TR>";
			}
			return tempHTML;
		}
		function GenerateReportContext()
		{
			var dataControl=window.opener.document.all(reportControl);			
			var dataGrid=dataControl.childNodes[1].cloneNode(true);
			dataGrid.deleteRow(0);
			
			for(r=0;r<dataGrid.rows.length;r++)
			{
				dataGrid.rows[r].removeAttribute("style");
				
				//���ز���Ҫ��ʾ����
				for(h=0;h<hiddenColumns.length;h++)
				{
					dataGrid.rows[r].cells[hiddenColumns[h]].style.display="none";
				}
			}
			var dataHTML=dataGrid.innerHTML;
			return dataHTML;
		}
		function FinishPrint()
		{
				alert("列印已經完成。");
				window.close();
		}
		function AbortPrint()
		{
			alert("由於無法載入需要的列印資料，列印失敗！\n您可以更改“報表設置”中的“資料讀取間隔時間”或“資料讀取重試次數”的參數值，然後再試一次。");
			ReportContextArray=new Array();
		}
		function PrintPage()
		{
			if(window.opener.document.readyState!="complete")
			{
				//�����ڻ�δ׼���ã�����
				if(RetryTimes<RetryTimesCount)
				{
					SetStatusInfo("載入列印資料...重試"+RetryTimes);
					RetryTimes++;
					setTimeout(PrintPage,printInterval);
				}
				else
				{
					AbortPrint();
				}
				return;
			}
			RetryTimes=0;
			SetStatusInfo("正在列印..."+currentPrintPage+"/"+totalPrintPage);
			doc = printFrame.document;
			doc.open();
			doc.write(GetReportContext(currentPrintPage));
			doc.close();
			printFrame.outerHTML = "";
			printFrame.focus();
			Printer.ExecWB(6,6);
				
			currentPrintPage++;
			if(currentPrintPage>totalPrintPage )
			{
				FinishPrint();
				return;
			}
			if(currentPrintPage>ReportContextArray.length-1)
			{
				var nextPageBtn=window.opener.document.all(reportControl+":_ctl28");
				nextPageBtn.click();	//前進到下一頁
				LoadedNewPage=true;
				historyPage++;
				setTimeout(PrintPage,printInterval);
			}
			else
			{
				PrintPage();
			}
		}
		function PreviewReport()
		{
			if(window.opener.document.readyState!="complete")
			{
				//�����ڻ�δ׼���ã�����
				if(RetryTimes<RetryTimesCount)
				{
					SetStatusInfo("載入列印資料...重試"+RetryTimes);
					RetryTimes++;
					setTimeout(PreviewReport,printInterval);
				}
				else
				{
					alert("載入列印資料失敗！");
					try
					{
						previewWin.close();
					}
					catch(e)
					{
					}
				}
				return;
			}
			RetryTimes=0;
			try
			{
				previewWin.SetPreviewContext(GetReportContext(currentPrintPage));
				previewWin.ShowWaitInfo(false);
			}
			catch(e)
			{
			}
		}
		function PreviewPreviousPage()
		{
			currentPrintPage--;
			try
			{
				previewWin.SetPreviewContext(GetReportContext(currentPrintPage));
			}
			catch(e)
			{
			}
		}
		function PreviewNextPage()
		{
			currentPrintPage++;
			if(currentPrintPage>ReportContextArray.length-1)
			{
				//ҪԤ����ҳ��û����
				var nextPageBtn=window.opener.document.all(reportControl+":_ctl28");
				if(currentPrintPage<=totalPrintPage)
				{
					nextPageBtn.click();	//前進到下一頁
					LoadedNewPage=true;
					historyPage++;
					setTimeout(PreviewReport,printInterval);
				}
			}
			else
			{
				PreviewReport();
			}
		}
		function SetStatusInfo(infoText)
		{
			document.all("StatusPanel").innerText=infoText;
		}
		
		//��ӡ
		function OnPrint()
		{
			if(!confirm("你確實要列印報表嗎？"))
				return;
			alert("準備列印報表，請確定印表機已準備好，點　確定　開始列印。列印過程根據資料的多少可能需要一段時間，在這期間請不要做任何操作，列印完畢時，系統會通知您。");
			IsPrintting=true;
			DisableBtn();
			currentPrintPage=1;
			
			CreatePrintArea();
			var jumpValue=window.opener.document.all(reportControl+":_ctl32");		//父表格“轉到”框
			var jumpBtn=window.opener.document.all(reportControl+":_ctl33");		//父表格“轉到”按鈕
			if(document.all("AllPage").checked)
			{
				//�����Ҫ��ӡ����ҳ
				if(gridCurrentPageIndex>1)
				{
					//�������񲢲����ڵ�һҳ������Ҫ��ת����һҳ
					jumpValue.value="1";
					jumpBtn.click();
					IsJumpPage=true;
					historyPage++;
				}
				totalPrintPage=gridTotalPageCount;
				setTimeout(PrintPage,printInterval);
			}
			else
			{
				if(document.all("AppointedPage").checked)
				{
					//��ӡָ����Χ��ҳ
					PrintStartPageNo=parseInt(document.all("PrintStartPageNo").value);
					PrintEndPageNo=parseInt(document.all("PrintEndPageNo").value)
					totalPrintPage=PrintEndPageNo-PrintStartPageNo+1;
					if(PrintStartPageNo!=gridCurrentPageIndex)
					{
						//�����ʼҳ�벻���ڸ����ĵ�ǰҳ�룬����Ҫ��ת��ָ����ҳ��
						jumpValue.value=PrintStartPageNo;
						jumpBtn.click();
						IsJumpPage=true;
						historyPage++;
						setTimeout(PrintPage,printInterval);
					}
					else
					{
						PrintPage();		//從當前頁開始列印
					}
				}
				else
				{
					//����ӡ��ǰҳ
					PrintPage();
				}
			}
		}
		
		//��ӡԤ��
		function OnPreview()
		{
			HiddenColumnCheck();
			if(previewWin!=null)
			{
				previewWin.close();
			}
			if(document.all("AllPage").checked)
			{
				totalPrintPage=gridTotalPageCount;
			}
			else
			{
				if(document.all("AppointedPage").checked)
				{
					PrintStartPageNo=parseInt(document.all("PrintStartPageNo").value);
					PrintEndPageNo=parseInt(document.all("PrintEndPageNo").value)
					totalPrintPage=PrintEndPageNo-PrintStartPageNo+1;
				}
				else
				{
					totalPrintPage=1
				}
			}
			currentPrintPage=1;
			previewWin=window.open("Preview.htm","","width=1000,height=750,status=no;menubar=no,resizable=yes");
		}
		function OnPageSetup()
		{
			Printer.ExecWB(8,1);
		}
		function OnReportSet()
		{
			window.showModalDialog("ReportSetting.htm",window,"dialogWidth:375px;dialogHeight:290px;help:no;status:no;");
		}
		//����Ƿ��в���Ҫ��ӡ����
		function HiddenColumnCheck()
		{
			var tempary;
			tempary=GetSelectItemIndex(document.all("checkboxList1"));
			if(hiddenColumns.length!=tempary.length && ReportContextArray.length!=0 )
			{
				//�Ѹ�����Ҫ���ص��У���Ҫ���������������ݡ�
				SetStatusInfo("正在隱藏列...");
				var l,r,h;
				var temptable;
				//���ѱ����������е���������Ӧ����
				for(l=1;l<ReportContextArray.length;l++)
				{
					document.body.insertAdjacentHTML("beforeEnd","<table id=temp>"+ReportContextArray[l]+"</table>")
					temptable=document.all("temp");
					for(r=0;r<temptable.rows.length;r++)
					{
						//�����µ���Ҫ���ص���
						for(h=0;h<tempary.length;h++)
						{
							if(!SearchElementFromArray(hiddenColumns,tempary[h]))
							{
								temptable.rows[r].cells[tempary[h]].style.display="none";
							}
						}
						//��ʾ�Ѳ���Ҫ���ص���
						for(h=0;h<hiddenColumns.length;h++)
						{
							if(!SearchElementFromArray(tempary,hiddenColumns[h]))
							{
								temptable.rows[r].cells[hiddenColumns[h]].style.display="inline";
							}
						}
					}
					ReportContextArray[l]=temptable.innerHTML;
					document.body.removeChild(temptable)
				}
				SetStatusInfo("就緒，按F1鍵獲取幫助");
			}
			hiddenColumns=tempary;
		}
		function IsPrintCurrentPage()
		{
			return document.all("OnlyCurrentPage").checked;
		}
		function Dispose()
		{
			if(previewWin!=null)
				previewWin.close();
			if(helpWin!=null)
				helpWin.close();
			if(IsJumpPage)
			{
				var jumpValue=window.opener.document.all(reportControl+":_ctl32");		//父表格“轉到”框
				jumpValue.value="";
			}
			if(LoadedNewPage && historyPage>0)
			{
				if(opener.window!=null)
				{
					try
					{
						opener.window.history.go(-historyPage)
					}
					catch(e)
					{
					}
				}
			}
			
		}
		function CheckInput()
		{
			temp=window.event.srcElement.value;
			no=parseInt(temp);
			if(no=="NaN")
			{
				alert("無效的頁碼。");
				window.event.srcElement.focus();
			}
			else
			{
				if(no<=0)
					window.event.srcElement.value=1
				if(no>gridTotalPageCount)
					window.event.srcElement.value=gridTotalPageCount;
			}
		}
		
	//������checklistbox��֧�ֽű�
	function AddListItem(checkboxList,text,value)
	{
		var list=checkboxList.childNodes[0];
		var row=list.insertRow();
		var cell=row.insertCell();
		cell.innerHTML="<input type=checkbox value='"+value+"'>"+text;
	}
	function CreateList(itemArray)
	{
		var i;
		for(i=0;i<itemArray.length;i++)
		{
			AddListItem(document.all("checkboxList1"),itemArray[i],itemArray[i]);
		}
	}
	function SearchElementFromArray(array,element)
	{
		var i;
		for(i=0;i<array.length;i++)
		{
			if(array[i]==element)
			return true;
		}
		return false;
	}
	function GetSelectedItem(checkboxList,mode)
	{
		var list=checkboxList.childNodes[0];
		var j=0,i;
		var selectedArray=new Array();
		for(i=0;i<list.rows.length;i++)
		{
			if(list.rows[i].cells[0].childNodes[0].checked)
			{
				switch(mode)
				{
					case "index":
						selectedArray[j]=i;
						break;
					case "value":
						selectedArray[j]=list.rows[i].cells[0].childNodes[0].value;
						break;
				}
				j++;
			}
		}
		return selectedArray;
	}
	function SetSelectedItem(checkboxList,valueArray,mode)
	{
		var list=checkboxList.childNodes[0];
		if(list.rows.length==0)
			return;
		var j=0;
		switch(mode)
		{
			case "index":
				for(i=0;i<valueArray.length;i++)
				{
					if(valueArray[i]<list.rows.length)
					{
						list.rows[i].cells[0].childNodes[0].checked=true;
					}
				}
				break;
			case "value":
				for(i=0;i<list.rows.length;i++)
				{
					if(SearchElementFromArray(valueArray,list.rows[i].cells[0].childNodes[0].value))
					{
						list.rows[i].cells[0].childNodes[0].checked=true;
					}
				}
				break;
		}
			
	}
	function GetSelectItemValue(checkboxList)
	{
		return GetSelectedItem(checkboxList,"value");
	}
	function GetSelectItemIndex(checkboxList)
	{
		return GetSelectedItem(checkboxList,"index");
	}
	function SetSelectedItemByValue(checkboxList,valueArray)
	{
		SetSelectedItem(checkboxList,valueArray,"value");
	}
	function SetSelectedItemByIndex(checkboxList,indexArray)
	{
		SetSelectedItem(checkboxList,indexArray,"index");
	}
	</script>
	</head>
	<body bgcolor="#F7F3EF" topmargin=0 leftmargin=0 rightmargin=0 bottommargin=0 onload="Initialize()" style="border:0" onunload="Dispose()">
		<OBJECT id="Printer" height="0" width="0" classid="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"
			 VIEWASTEXT>
		</OBJECT>
		<table width="100%" height="52" bgcolor="white" background="images/report.gif" style="BORDER-RIGHT: white thin inset; BORDER-TOP: thin inset; BORDER-LEFT: thin inset; BORDER-BOTTOM: white thin inset">
			<tr>
				<td></td>
			</tr>
		</table>
		<fieldset>
			<legend><font size=2>列印頁範圍</font></legend>
			<table width=100%>
				<tr>
					<td><input type=radio name="PrintRange" value="0" id="OnlyCurrentPage" onclick="document.all('PrintStartPageNo').disabled=document.all('PrintEndPageNo').disabled=true;"><label for="OnlyCurrentPage">僅當前頁</label></td>
					<td><input type=radio name="PrintRange" value="1" id="AllPage" checked=true onclick="document.all('PrintStartPageNo').disabled=document.all('PrintEndPageNo').disabled=true;"><label for="AllPage">所有頁</label></td>
					<td><input type=radio name="PrintRange" value="2" id="AppointedPage" onclick="document.all('PrintStartPageNo').disabled=document.all('PrintEndPageNo').disabled=false;"><label for="AppointedPage">指定頁碼</label>&nbsp;<input type=text style="width:40px" id="PrintStartPageNo" disabled=true onblur="CheckInput()">到<input type=text style="width:40px" id="PrintEndPageNo" disabled=true onblur="CheckInput()">頁</td>
				</tr>
			</table>
		</fieldset>
		<fieldset>
			<legend><font size=2>不需要列印的列</font></legend>
			<div  id="checkboxList1" style="background-color:white;overflow-y:scroll;width:100%;height:70px;BORDER-RIGHT: white thin inset; BORDER-TOP: buttonface thin inset; BORDER-LEFT: buttonface thin inset; BORDER-BOTTOM: white thin inset">
				<table width=100% ID="Table1">
				</table>
			</div>
		</fieldset>
		<table width=100% height=30>
			<tr>
				<td width=25%  id="Btn_PageSet"align=center valign=absmiddle onmouseover="ButtonStyleSet('3D',this);if(!IsPrintting) SetStatusInfo('列印頁面設置，選擇印表機...');" onmouseout="ButtonStyleSet('Flat',this);if(!IsPrintting) SetStatusInfo('就緒，按F1鍵獲取幫助')" onmousedown="ButtonStyleSet('Cancave',this)" Enabled="true" onclick="OnPageSetup()"><img src="images/pagesetup.gif">頁面設置</td>
				<td width=25%  id="Btn_Preview" align=center  valign=absmiddle onmouseover="ButtonStyleSet('3D',this);if(!IsPrintting) SetStatusInfo('預覽要列印的內容');" onmouseout="ButtonStyleSet('Flat',this);if(!IsPrintting) SetStatusInfo('就緒，按F1鍵獲取幫助')" onmousedown="ButtonStyleSet('Cancave',this)" onclick="OnPreview()" Enabled="true"><img src="images/preview.gif">列印預覽</td>
				<td width=25%  id="Btn_Print" align=center  valign=absmiddle onmouseover="ButtonStyleSet('3D',this);if(!IsPrintting) SetStatusInfo('開始列印');" onmouseout="ButtonStyleSet('Flat',this);if(!IsPrintting) SetStatusInfo('就緒，按F1鍵獲取幫助')" onmousedown="ButtonStyleSet('Cancave',this)" onclick="OnPrint()" Enabled="true"><img src="images/print.gif">打　　印</td>
				<td width=25%  id="Btn_ReportSet" align=center  valign=absmiddle onmouseover="ButtonStyleSet('3D',this);if(!IsPrintting) SetStatusInfo('設置報表的樣式和參數');" onmouseout="ButtonStyleSet('Flat',this);if(!IsPrintting) SetStatusInfo('就緒，按F1鍵獲取幫助')" onmousedown="ButtonStyleSet('Cancave',this)" onclick="OnReportSet()" Enabled="true"><img src="images/reportset.gif">報表設置</td>
			</tr>
		</table>
		<table width=100%>
			<tr>
				<td style="BORDER-RIGHT: 1px inset; BORDER-TOP: 1px inset; BORDER-LEFT: 1px inset; BORDER-BOTTOM: 1px inset" id="StatusPanel">就緒，按F1鍵獲取幫助</td>
			</tr>
		</table>
	</body>
</html>
